package com.wang.springbootinit.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.springbootinit.common.ErrorCode;
import com.wang.springbootinit.exception.BusinessException;
import com.wang.springbootinit.mapper.ResearchSubjectMapper;
import com.wang.springbootinit.model.entity.ResearchSubject;
import com.wang.springbootinit.model.entity.ResearchUniversity;
import com.wang.springbootinit.service.ResearchSubjectService;
import com.wang.springbootinit.service.ResearchUniversityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * 章节
 */
@Service
public class ResearchSubjectServiceImpl extends ServiceImpl<ResearchSubjectMapper, ResearchSubject> implements ResearchSubjectService {

    @Autowired
    private ResearchUniversityService researchUniversityService;

    /**
     * 根据学校id查询下面的所有科目
     * @param schoolId 学校id
     * @return
     */
    @Override
    public List<ResearchSubject> listSubjectsBySchoolId(Long schoolId) {
        ResearchUniversity university = researchUniversityService.getById(schoolId);
        if(ObjectUtil.isNull(university)){
            throw new BusinessException(ErrorCode.UNIVERSITY_NOT_EXIST_ERROR);
        }

        LambdaQueryWrapper<ResearchSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ResearchSubject::getUniversityId,university.getId());
        List<ResearchSubject> researchSubjectList = this.list(queryWrapper);
        if(ObjectUtil.isNotEmpty(researchSubjectList)){
            return researchSubjectList;
        }
        return Collections.emptyList();
    }
}
